package top.zsyle.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zsyle.basic.query.BaseQuery;
import top.zsyle.basic.util.PageList;
import top.zsyle.system.aop.LogAop;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import top.zsyle.org.domain.Department;
import top.zsyle.org.mapper.DepartmentMapper;
import top.zsyle.org.service.IDepartmentService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @LogAop
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    @LogAop(name = "嗷嗷")
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    @LogAop(name = "删删",age = 2)
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    @LogAop
    public void update(Department dept) {
        departmentMapper.update(dept);
    }

    @Override
    @LogAop
    public void add(Department dept) {
        departmentMapper.add(dept);
    }


    @Override
    @LogAop(name = "什么东西",age = 2)
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }

    /**
     * 无限级树（高级玩法-必须掌握）
     *
     * @return
     */
    @Override
    @LogAop(name = "无限级树",age = 2)
    public List<Department> tree() {
        List<Department> allDepartments = departmentMapper.findAll();

        ArrayList<Department> result = new ArrayList<>();
        //一般避免嵌套for循环都是使用map
        //Map<Long, Department> deptMap = allDepartments.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        //或则这种写法
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department dept : allDepartments) {
            deptMap.put(dept.getId(),dept);
        }
        for (Department dept : allDepartments) {

            if (null == dept.getParentId()) {
                //这就是顶级部门
                result.add(dept);
            } else {
                //都是有上级部门的
                //高级玩法
                Department parent = deptMap.get(dept.getParentId());
                if(parent != null){
                    parent.getChildren().add(dept);
                }
            }
        }
        return result;
    }

    /**
     * 无限级树（简单玩法）
     * @return
     */
//    @Override
//    public List<Department> tree() {
//
//        List<Department> allDepartments = departmentMapper.findAll();
//        ArrayList<Department> result = new ArrayList<>();
//        for (Department dept : allDepartments) {
//            if (null == dept.getParentId()){
//                //这就是顶级部门
//                result.add(dept);
//            }else{
//                //都是有上级部门的
//                //这是简单玩法（效率低下....）
//                for (Department parent : allDepartments) {
//                    if (dept.getParentId().equals(parent.getId())){
//                        parent.getChildren().add(dept);
//                        break;//退出循环（找到上级部门就没必要找了）
//                    }
//                }
//            }
//        }
//        return result;
//    }


}
